import { toNumLocaleString } from 'microconnect-common/utils/math';
import { isNotData } from './is';

/**
 * 转换为接口sort请求的格式
 * @param prop 排序字段
 * @param order el-table 排序方式
 * @returns 字段,desc || asc
 * @example
 * // 有值时
 * patchOrder('num', 'descending') => 'num,desc'
 * // 无值时
 * patchOrder('', '') => ''
 */
export const patchTableOrder = (prop: any, order: string) => {
  let newOrder = '';
  if (order) {
    newOrder = order === 'descending' ? 'desc' : 'asc';
  }
  return [prop, newOrder].filter(Boolean).join(',');
};

/**
 * 千分位, 保留两位小数
 */
export const toNumLocalFormatter = (row: any, column: any, cellValue: any) => {
  if (isNotData(cellValue)) return '-';
  return toNumLocaleString(cellValue, 2);
};

/**
 * 转换为接口sort请求的格式，驼峰命名转下划线分割
 * @param prop 排序字段
 * @param order el-table 排序方式
 * @returns 字段,desc || asc
 */
export const patchTableOrderByHumpToLine = (prop: any, order: string) => {
  if (!prop) prop = '';
  let newOrder = '';
  let switchProp = prop.replace(/([A-Z])/g, '_$1').toLowerCase();
  if (order) {
    newOrder = order === 'descending' ? 'desc' : 'asc';
  }
  return [switchProp, newOrder].filter(Boolean).join(',');
};
